package com.spider.admin.controller;
import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.ClientEntity;
import com.spider.admin.http.entity.TokenEntity;
import com.spider.admin.service.account.AccountService;
import com.spider.admin.service.client.PublishClientTenantsService;
import com.spider.admin.service.token.TokenService;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录Controller
 *
 * @author Administrator
 */
@Slf4j
@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private PublishClientTenantsService publishClientTenantsService;
    /**
     * 获取Token
     */
    @PostMapping("/token")
    public Map<String,String> token(@RequestBody @NotNull ClientEntity client)throws SpiderException {
        return tokenService.createToken(client.getClientKey());
    }

    /**
     * 验证Token
     * @param token
     * @return
     */
    @PostMapping("/verify")
    public Map<String, Object> verify(@RequestBody TokenEntity token)throws SpiderException {
        return tokenService.verify(token);
    }


    /**
     * 更新Redis缓存
     */
    @PutMapping("/redis/update")
    public void updateRedis()throws SpiderException{
        publishClientTenantsService.putToCacheForPerms();
    }
}



